package com.song.studylineserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.song.studylineserver.domain.Menu;
import com.song.studylineserver.service.MenuService;
import com.song.studylineserver.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "菜单")
@RestController
@RequestMapping("/Menu")
public class MenuController {
    @Autowired
   MenuService menuService;

    /**
     * 获取用户权限列表
     * @param userId
     * @return
     */
    @Operation(summary = "测试")
    @RequestMapping("list")
    public List<String> selectMenuPermsByUserId(Long userId)
    {
        List<String> perms = menuService.selectMenuPermsByUserId(userId);
        List<String> permsList = new ArrayList<>();
        for(String perm:perms)
        {
            permsList.addAll(Arrays.asList(perm.trim().split(",")));
        }
        return  permsList;
    }


    /**
     * 获取菜单列表
     */
    @ApiOperation("获取菜单列表")
    @GetMapping("list")
    public Result list(Menu menu)
    {
        Long userId = Convert.toLong(StpUtil.getLoginId());
        List<Menu> menus = menuService.selectMenuList(menu, userId);
        return new Result(200,"查询菜单列表",menus);
    }

    @ApiOperation("获取路由")
    @RequestMapping("getRouters")
    public Result getRouters()
    {

        Long userId = Convert.toLong(StpUtil.getLoginId());
        List<Menu> menus =menuService.selectMenuTreeByUserId(userId);
        return new Result(200,"查询路由成功！",menuService.buildMenus(menus));

    }




}
